package com.pethome.web.controller;

import com.pethome.common.annotation.Log;
import com.pethome.common.core.controller.BaseController;
import com.pethome.common.core.domain.AjaxResult;
import com.pethome.common.core.page.TableDataInfo;
import com.pethome.common.enums.BusinessType;
import com.pethome.common.utils.poi.ExcelUtil;
import com.pethome.web.domain.News;
import com.pethome.web.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 信息管理Controller
 * 
 * @author hjc
 * @date 2024-08-26
 */
@RestController
@RequestMapping("/web/news")
public class NewsController extends BaseController
{
    @Autowired
    private INewsService newsService;


    @GetMapping("/plussXinWenPower")
    public Boolean plussXinWenPower(@RequestParam("newsId") Long newsId){
        return newsService.plussXinWenPower(newsId)==1;
    }



    /**
     * 查看最近的10条新闻
     */
    @GetMapping("/getXinWen")
    public TableDataInfo getXinWen(@RequestParam("type") Integer type)
    {
        List<News> list = newsService.getXinWenList(type);
        return getDataTable(list);
    }

    /**
     * 通过标题查找新闻
     * @return
     */
    @GetMapping("/searchXinWenByTitle")
    public TableDataInfo searchXinWenByTitle(@RequestParam("title") String title){
        List<News> list = newsService.searchXinWenByTitle(title);
        return getDataTable(list);
    }



    /**
     * 查询信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('web:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(News news)
    {
        startPage();
        List<News> list = newsService.selectNewsList(news);
        return getDataTable(list);
    }

    /**
     * 导出信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('web:news:export')")
    @Log(title = "信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, News news)
    {
        List<News> list = newsService.selectNewsList(news);
        ExcelUtil<News> util = new ExcelUtil<News>(News.class);
        util.exportExcel(response, list, "信息管理数据");
    }

    /**
     * 获取信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:news:query')")
    @GetMapping(value = "/{newsId}")
    public AjaxResult getInfo(@PathVariable("newsId") Long newsId)
    {
        return success(newsService.selectNewsByNewsId(newsId));
    }

    /**
     * 新增信息管理
     */
    @PreAuthorize("@ss.hasPermi('web:news:add')")
    @Log(title = "信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody News news)
    {
        news.setNewsPower(0L);
        return toAjax(newsService.insertNews(news));
    }

    /**
     * 修改信息管理
     */
    @PreAuthorize("@ss.hasPermi('web:news:edit')")
    @Log(title = "信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody News news)
    {
        return toAjax(newsService.updateNews(news));
    }

    /**
     * 删除信息管理
     */
    @PreAuthorize("@ss.hasPermi('web:news:remove')")
    @Log(title = "信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{newsIds}")
    public AjaxResult remove(@PathVariable Long[] newsIds)
    {
        return toAjax(newsService.deleteNewsByNewsIds(newsIds));
    }
}
